import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_common_app/api/base_api.dart';
import 'package:flutter_common_app/utils/common_util/log_util.dart';

///加载状态
enum LoadState {
  Success,
  Error,
  Empty,
  Loading,
}

///view 包装器
class LoadingViewWrapper extends StatefulWidget {
  final BaseApi baseApi;
  final String mapKey;
  final Widget Function(BuildContext context, Response res) builder;

  const LoadingViewWrapper(
    this.baseApi,
    this.builder, {
    Key key,
    this.mapKey = 'data',
  }) : super(key: key);

  @override
  _LoadingViewWrapperState createState() => _LoadingViewWrapperState();
}

class _LoadingViewWrapperState extends State<LoadingViewWrapper> {
  LoadState _loadState = LoadState.Loading;

  Response response;

  Widget successView;

  @override
  void initState() {
    getData();
    super.initState();
  }

  void getData() {
    widget.baseApi.send().then((res) {
      if (mounted) {
        setState(() {
          if (res.data[widget.mapKey].length > 0) {
            _loadState = LoadState.Success;
            response = res;
          } else {
            _loadState = LoadState.Empty;
          }
        });
      }
    }).catchError((e) {
      LogUtil.e('LoadingViewWrapper get data error:$e');
      setState(() {
        _loadState = LoadState.Error;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    if (successView != null) {
      return successView;
    }

    Widget contentView = Container();

    switch (_loadState) {
      case LoadState.Empty:
        contentView = buildView(
          Center(
            child: Text('数据为空'),
          ),
        );
        break;
      case LoadState.Success:
        contentView = widget.builder(context,response);
        successView = contentView;
        break;
      case LoadState.Loading:
        contentView = buildView(
          Center(
            child: Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                CupertinoActivityIndicator(),
                Text('加载中'),
              ],
            ),
          ),
        );
        break;
      case LoadState.Error:
        contentView = buildView(
          GestureDetector(
            onTap: (){
              setState(() {
                _loadState = LoadState.Loading;
                getData();
              });
            },
            child: Center(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Icon(Icons.error),
                  Text('出错'),
                ],
              ),
            ),
          ),
        );
        break;
      default:
        break;
    }
    return contentView;
  }

  Widget buildView(Widget child) {
    return child;
  }
}
